package net.gzw1993.runmate.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;

import net.gzw1993.runmate.R;
import net.gzw1993.runmate.adapter.LineActivityAdapter;
import net.gzw1993.runmate.bean.ActiveInfo;
import net.gzw1993.runmate.bean.Content;
import net.gzw1993.runmate.constant.UrlInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link LineActivityFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class LineActivityFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener{

    private View view;
    @ViewInject(R.id.line_fragment_swipeRefresh)
    private SwipeRefreshLayout line_fragment_swipeRefresh;
    @ViewInject(R.id.line_fragment_listView)
    private ListView line_fragment_listView;
    private List<Content> cotents = new ArrayList<>();
    HttpUtils httpUtils = new HttpUtils();
    private HttpHandler httpHandler;
    private LineActivityAdapter adapter;


    public static LineActivityFragment newInstance() {
        LineActivityFragment fragment = new LineActivityFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public LineActivityFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_line_activity, container, false);
        ViewUtils.inject(this,view);
        initView();
        initData();
        return view;
    }

    /**
     * 初始化控件
     */
    private void initView() {
        line_fragment_swipeRefresh.setOnRefreshListener(this);  //为SwipeRefreshLayout设置刷新回调方法
        adapter = new LineActivityAdapter(getActivity());
        line_fragment_listView.setAdapter(adapter);
    }

    private void initData() {

        httpHandler = httpUtils.send(HttpRequest.HttpMethod.GET, UrlInfo.getOnlineActive(), new RequestCallBack() {
            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                String result = (String) responseInfo.result;
                ActiveInfo info = JSONObject.parseObject(result, ActiveInfo.class);
                /**
                 * 如果cotents里面有数据 且cotents的第一条数据的ID与请求下来第一条数据的ID相同。直接停止刷新
                 */
                if(cotents.size()>0 && cotents.get(0).getId().equals(info.getContent().get(0).getId())){
                    /**
                     * 如果是第一次进来或者cotents里面没有数据，我就把请求下来的数据通过addItem方法给加进去，刷新停止
                     */
                }else if(cotents.size() == 0){
                    cotents.addAll(info.getContent());
                    adapter.addItems(cotents);
                    /**
                     * 如果上述两个条件不满足  说明有新数据下来，那么调用replace方法直接把请求下来的数据赋值给mList，即替换。
                     */
                }else{
                    cotents.addAll(info.getContent());
                    adapter.replace(cotents);
                }
                line_fragment_swipeRefresh.setRefreshing(false);
            }
            @Override
            public void onFailure(HttpException e, String s) {
            }
        });
    }

    /**
     * SwipeRefreshLayout刷新回调方法
     */
    @Override
    public void onRefresh() {
        initData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        httpHandler.cancel();
    }
}
